package cn.edu.sdjzu.xg.eagle.service;

import cn.edu.sdjzu.xg.eagle.entity.Department;
import cn.edu.sdjzu.xg.eagle.exception.BysjException;
import cn.edu.sdjzu.xg.eagle.inter.SchoolDaoInterface;
import cn.edu.sdjzu.xg.eagle.util.CommonDao;
import cn.edu.sdjzu.xg.eagle.util.JdbcHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DepartmentService {
    private static DepartmentService instance;
    private DepartmentService() {
        CommonDao.getInstance();
        SchoolDaoInterface.getInstance();
        SchoolService.getInstance();
    }
    // 修改 DepartmentService 中的 find 方法
    public Department find(int departmentId) throws BysjException {
        // 假设通过 JdbcHelper 查询 Department 表
        String sql = "SELECT * FROM department WHERE id = ?";
        try (Connection connection = JdbcHelper.getConn();
             PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setInt(1, departmentId);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                return new Department(id, name); // 返回 Department 对象
            }
        } catch (SQLException e) {
            throw new BysjException();
        }
        return null; // 如果未找到对应部门，返回 null
    }

    public static SchoolDaoInterface getInstance() {
        if (instance == null) {
            instance = new DepartmentService();
        }
        return (SchoolDaoInterface) instance;
    }
}
